#include <iostream>
#include<vector>
using namespace std;
int result(int n,string& s1,string& s2,vector<string>& strs) {
	vector<int> nums1;
	vector<int> nums2;
	
	for (int i = 0; i < strs.size(); i++) {
		if (strs[i] == s1) {
			nums1.push_back(i);
		}
	}
	for (int i = 0; i < strs.size(); i++) {
		if (strs[i] == s2) {
			nums2.push_back(i);
		}
	}
	vector<int> min;
	for (size_t i = 0; i < nums1.size(); i++)
	{
		for (size_t j = 0; j < nums2.size(); j++)
		{
			min.push_back(nums1[i] - nums2[j]);
		}
	}
	for (size_t i = 0; i < min.size(); i++)
	{
		for (size_t j = i; j < min.size()-1; j++)
		{
			if (min[j] > min[j + 1]) {
				swap(min[j], min[j + 1]);
			}
		}
	}
	return min[0];
}

int main() {
	int n;
	vector<string> strs;
	string s1;
	string s2;
	cin >> n;
	cin >> s1 >> s2;
	for (size_t i = 0; i < n; i++)
	{
		cin >> strs[i];
	}
	int ret = result(n, s1, s2, strs);
	cout << ret << endl;
	return 0;
}